/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package {{package}};

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.solr.client.solrj.response.json.JacksonDataBindResponseParser;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.response.SimpleSolrResponse;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.client.solrj.request.json.JacksonContentWriter;
import org.apache.solr.client.solrj.request.RequestWriter.ContentWriter;
import org.apache.solr.client.solrj.response.InputStreamResponseParser;
import org.apache.solr.client.solrj.response.InputStreamResponse;
import org.apache.solr.client.solrj.response.ResponseParser;
import org.apache.solr.common.util.NamedList;

{{! Covers all top-level request/response model classes, but not necessarily any types nested in those classes }}
{{#imports}}
import {{import}};
{{/imports}}

{{! Imports any model types nested in request body POJOs }}
{{#operations}}
{{#operation}}
{{#bodyParam}}
{{#vars}}

{{! Catches all model types used "directly" (i.e. not in a container) in request/response classes. }}
{{#isModel}}
import {{modelPackage}}.{{dataType}};
{{/isModel}}

{{! Catches all model types used in a List<Foo>, Map<Bar>, or other container in request/response classes. }}
{{#isContainer}}
{{#items}}
{{#isModel}}
import {{modelPackage}}.{{dataType}};
{{/isModel}}
{{/items}}
{{/isContainer}}

{{/vars}}
{{/bodyParam}}
{{/operation}}
{{/operations}}

{{#operations}}
// WARNING: This class is generated from a Mustache template; any intended
// changes should be made to the underlying template and not this file directly.

/**
 * Experimental {@link SolrRequest}s for {{classVarName}}, generated from an OAS.
 *
 * <p>See individual request and response classes for more detailed and relevant information.
 *
 * <p>All SolrRequest implementations rely on v2 APIs which may require a SolrClient configured to
 * use the '/api' path prefix, instead of '/solr'.
 *
 * @lucene.experimental
 */
public class {{classname}} {

    {{#operation}}
    {{^vendorExtensions.x-omitFromCodegen}}
        {{#vendorExtensions.x-rawOutput}}
            public static class {{operationIdCamelCase}} extends SolrRequest
          <InputStreamResponse> {
        {{/vendorExtensions.x-rawOutput}}
        {{^vendorExtensions.x-rawOutput}}
            public static class {{operationIdCamelCase}} extends SolrRequest<{{modelPackage}}
            .{{returnType}}> {
        {{/vendorExtensions.x-rawOutput}}
            {{#bodyParam}}
            private final {{{dataType}}} requestBody;
            {{/bodyParam}}
            {{#requiredParams}}
            {{^isBodyParam}}
            private final {{{dataType}}} {{paramName}};
            {{/isBodyParam}}
            {{/requiredParams}}
            {{#optionalParams}}
            {{^isBodyParam}}
            private {{{dataType}}} {{paramName}};
            {{/isBodyParam}}
            {{/optionalParams}}

            /**
             * Create a {{operationIdCamelCase}} request object.
             *
             {{#requiredParams}}{{^isBodyParam}}* @param {{paramName}} Path param - {{description}}{{/isBodyParam}}
             {{/requiredParams}}
             */
            public {{operationIdCamelCase}}({{#allParams}}{{#required}}{{^isBodyParam}}{{^-first}}, {{/-first}}{{{dataType}}} {{paramName}}{{/isBodyParam}}{{#isBodyParam}}{{#vendorExtensions.x-genericEntity}}{{^-first}}, {{/-first}}{{{dataType}}} requestBody{{/vendorExtensions.x-genericEntity}}{{/isBodyParam}}{{/required}}{{/allParams}}) {
                // TODO Hardcode request type for now, but in reality we'll want to parse this out of the Operation data somehow
                super(
                  SolrRequest.METHOD.valueOf("{{httpMethod}}"),
                  "{{{path}}}"{{#pathParams}}
                    .replace("{" + "{{baseName}}" + "}", {{paramName}}{{^isString}}.toString(){{/isString}}){{/pathParams}},
                    SolrRequestType.ADMIN
                );

                {{#requiredParams}}
                {{#isBodyParam}}
                {{#vendorExtensions.x-genericEntity}}
                    this.requestBody = requestBody;
                {{/vendorExtensions.x-genericEntity}}
                {{/isBodyParam}}
                {{^isBodyParam}}
                    this.{{paramName}} = {{paramName}};
                {{/isBodyParam}}
                {{/requiredParams}}
                {{#bodyParam}}
                {{^vendorExtensions.x-genericEntity}}
                {{#isMap}}
                    this.requestBody = new HashMap<>();
                {{/isMap}}
                {{#isArray}}
                    this.requestBody = new ArrayList<>();
                {{/isArray}}
                {{^isMap}}
                {{^isArray}}
                    this.requestBody = new {{{dataType}}}();
                {{/isArray}}
                {{/isMap}}
                {{/vendorExtensions.x-genericEntity}}
                {{/bodyParam}}
            }

            {{! If the request body is a list, provide methods to add to it}}
            {{#bodyParam}}
            {{#isArray}}
            public void add{{items.dataType}}({{items.dataType}} entry) {
                this.requestBody.add(entry);
            }
            {{/isArray}}
            {{/bodyParam}}

            {{#optionalParams}}
            {{^isBodyParam}}
            {{#description}}
            /**
             * @param {{paramName}} {{description}}
             */
            {{/description}}
            public void {{schema.setter}}({{{dataType}}} {{paramName}}) {
                this.{{paramName}} = {{paramName}};
            }

            {{/isBodyParam}}
            {{/optionalParams}}

            {{#bodyParam}}
            {{#vars}}
            // TODO find a way to add required parameters in the request body to the class constructor
            {{#description}}
            /**
             * @param {{baseName}} {{description}}
             */
             {{/description}}
             public void {{setter}}({{{dataType}}} {{baseName}}) {
               this.requestBody.{{baseName}} = {{baseName}};
             }
            {{/vars}}

            {{#vendorExtensions.x-hasAdditionalFields}}
            /**
             * Set additional properties to be included in the request body.
             */
            public void setAdditionalProperty(String property, Object value) {
                requestBody.setAdditionalProperty(property, value);
            }
            {{/vendorExtensions.x-hasAdditionalFields}}

            @Override
            @SuppressWarnings("unchecked")
            public RequestWriter.ContentWriter getContentWriter(String _expectedTypeIGNORE) {
                // v2 only supports JSON request bodies, so we ignore this type coming from the RequestWriter
                {{#vendorExtensions.x-genericEntity}}
                return new RequestWriter.ContentWriter() {
                  @Override
                  public void write(OutputStream os) throws IOException {
                    ((InputStream) requestBody).transferTo(os);
                  }

                  @Override
                  public String getContentType() {
                    return "application/octet-stream";
                  }
                };
                {{/vendorExtensions.x-genericEntity}}
                {{^vendorExtensions.x-genericEntity}}
                return new JacksonContentWriter(requestBody);
                {{/vendorExtensions.x-genericEntity}}
            }
            {{/bodyParam}}

            @Override
            public ApiVersion getApiVersion() {
              return ApiVersion.V2;
            }

            @Override
            public SolrParams getParams() {
              final ModifiableSolrParams params = new ModifiableSolrParams();
              {{#queryParams}}
              if ({{paramName}} != null) {
                  {{#isArray}}{{paramName}}.forEach(v -> params.add("{{baseName}}", v{{^items.isString}}.toString(){{/items.isString}}));{{/isArray}}
                  {{^isArray}}params.add("{{baseName}}", {{paramName}}{{^isString}}.toString(){{/isString}});{{/isArray}}
              }
              {{/queryParams}}
              return params;
            }

            @Override
            public Set<String> getQueryParams() {
              final var queryParams = new HashSet<String>();
              {{#queryParams}}
              queryParams.add("{{baseName}}");
              {{/queryParams}}
              return queryParams;
            }
        {{#vendorExtensions.x-rawOutput}}
            @Override
          protected InputStreamResponse createResponse(NamedList
          <Object> namedList) {
            return new InputStreamResponse();
            }

            @Override
            public InputStreamResponseParser getResponseParser() {
            return new InputStreamResponseParser("json");
            }
        {{/vendorExtensions.x-rawOutput}}
        {{^vendorExtensions.x-rawOutput}}
            @Override
            protected {{modelPackage}}.{{returnType}} createResponse(NamedList
          <Object> namedList) {
            return ({{modelPackage}}.{{returnType}}) namedList.get("response");
            }

            @Override
            public ResponseParser getResponseParser() {
            return new JacksonDataBindResponseParser<>({{modelPackage}}.{{returnType}}.class);
            }
        {{/vendorExtensions.x-rawOutput}}

        }
    {{/vendorExtensions.x-omitFromCodegen}}
    {{/operation}}
}
{{/operations}}
